package com.wyh.spring.demo.controller;

import com.wyh.spring.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 请求.传参
 * User: 王圆豪
 * Date: 2025-06-01
 * Time: 14:49
 */

@RestController
@RequestMapping("request")
public class RequestController {
    @RequestMapping("d1")
    public String d1(String keyWord){
        return "接收参数：" + keyWord;
    }
    @RequestMapping("d2")
    public String d2(){
        return "111";
    }

    @RequestMapping("d3")
    public String d3(String userName, String passWord){
        return "userName = " + userName + "passWord = " + passWord;
    }

    @RequestMapping("d4")
    public String d4(Integer num) {
        return "接收参数：" + num;
    }

    //不传参时服务器会报错，因为int不能接收null
    @RequestMapping("d5")
    public String d5(int num){
        return "接收参数：" + num;
    }

    //传对象
    @RequestMapping("d6")
    public String d6(UserInfo userInfo){
        return "userInfo = " + userInfo.toString();
    }

    @RequestMapping("d7")
    public String d7(@RequestParam(value = "q", required = false)String keyWord){
        return "接收参数" + keyWord;
    }

    //传数组
    @RequestMapping("d8")
    public String d8(int[] arr){
        return Arrays.toString(arr);
    }

    //传集合
    @RequestMapping("d9")
    public String d9( List list){
        return list.toString();
    }

    //传JSON字符串
    @RequestMapping("d10")
    public String d10(@RequestBody UserInfo userInfo){
        return userInfo.toString();
    }

    //从URL中获取参数
    @RequestMapping("d11/{type}/{id}")
    public String d11( @PathVariable String type,@PathVariable Integer id){
        return "id:" + id + "type:" + type;
    }

    //上传文件
    @RequestMapping("/d12")
    public String d12(@RequestPart("filename") MultipartFile file) throws IOException {
        file.transferTo(new File("D:\\note_pic\\" + file.getOriginalFilename()));
        return "文件上传成功！";
    }

    //获取Cookie
    @RequestMapping("/d13")
    public String d13(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie c:
                    cookies) {
                System.out.println(c.getName() + ":" + c.getValue());
            }
        }
        return "获取Cookie成功！";
    }

    @RequestMapping("d14")
    public String d14(@CookieValue("name") String name){
        return "从Cookie中获得name：" + name;
    }

    //设置Session
    @RequestMapping("d15")
    public String d15(HttpServletRequest request){
        //获取Session对象
        HttpSession session = request.getSession();
        session.setAttribute("UserName","wyh");
        session.setAttribute("age", 18);
        return "设置Session成功！";
    }

    //获取Session
    @RequestMapping("d16")
    public String d16(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if (session == null){
            return "没有当前账号！";
        }else {
            String attribute = (String) session.getAttribute("UserName");
            return "当前登录用户：" + attribute;
        }
    }
    //设置Session
    @RequestMapping("d17")
    public String d17(HttpSession session){
        //获取Session对象
        session.setAttribute("UserName","wyh");
        session.setAttribute("age", 18);
        return "设置Session成功！";
    }

    //获取Session
    @RequestMapping("d18")
    public String d18(HttpSession session){
        String attribute = (String) session.getAttribute("UserName");
        return "当前登录用户：" + attribute;
    }

    //设置Session
    @RequestMapping("d19")
    public String d19(HttpSession session){
        //获取Session对象
        session.setAttribute("UserName","wyh");
        session.setAttribute("age", 18);
        return "设置Session成功！";
    }

    //获取Session
    @RequestMapping("d20")
    public String d20(@SessionAttribute("UserName") String name){
        return "当前登录用户：" + name;
    }

    /**
     * 获取header中的内容
     * @return
     */

    //传统方法
    @RequestMapping("d21")
    public String d21(HttpServletRequest request){
        String header = request.getHeader("User-Agent");
        return "从header中就获取的User-Agent" + header;
    }

    //简洁方法
    @RequestMapping("d22")
    public String d22(@RequestHeader("User-Agent") String header){
        return "从header中就获取的User-Agent" + header;
    }

}
